package com.hiyiin.IntelligentHospital.hosp.service;

import com.alibaba.fastjson.JSONObject;
import com.hiyiin.IntelligentHospital.cmn.client.DictFeignClient;
import com.hiyiin.IntelligentHospital.hosp.repository.HospitalRepository;
import com.hiyiin.IntelligentHospital.model.hosp.Hospital;
import com.hiyiin.IntelligentHospital.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HospitalServiceImpl implements HospitalService{
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;
    @Autowired
    private DepartmentService departmentService;

    @Override
    public void save(Map<String, Object> paramMap) {
        //把map集合转换为Hospital对象
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString,Hospital.class);

        //判断是否存在相同的数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.findHospitalByHoscode(hoscode);

        if(hospitalExist != null){  //存在，更新
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else {                     //不存在，添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.findHospitalByHoscode(hoscode);
        return hospital;
    }

    //医院列表(条件查询分页)
    @Override
    public Page<Hospital> selectHospPage(Integer currentPage, Integer pageSize, HospitalQueryVo hospitalQueryVo) {
        //创建pageable对象
        Pageable pageable = PageRequest.of(currentPage-1, pageSize);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        Example<Hospital> hospitalExample = Example.of(hospital,matcher);
        Page<Hospital> page = hospitalRepository.findAll(hospitalExample, pageable);
        List<Hospital> list = page.getContent();
        list.forEach(hosp ->{
            String hostypeString = dictFeignClient.getName("Hostype", hosp.getHostype());
            String privinceString = dictFeignClient.getName(hosp.getProvinceCode());
            String cityString = dictFeignClient.getName(hosp.getCityCode());
            String districtString = dictFeignClient.getName(hosp.getDistrictCode());
            hosp.getParam().put("hostypeString",hostypeString);
            hosp.getParam().put("fullAddressString",privinceString+cityString+districtString);
        });
        return page;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String,Object> getHospById(String id) {
        Hospital hosp = hospitalRepository.findById(id).get();
        String hostypeString = dictFeignClient.getName("Hostype", hosp.getHostype());
        String privinceString = dictFeignClient.getName(hosp.getProvinceCode());
        String cityString = dictFeignClient.getName(hosp.getCityCode());
        String districtString = dictFeignClient.getName(hosp.getDistrictCode());
        hosp.getParam().put("hostypeString",hostypeString);
        hosp.getParam().put("fullAddressString",privinceString+cityString+districtString);

        Map<String,Object> result = new HashMap<>();
        //医院基本信息
        result.put("hospital",hosp);
        //单独处理更直观
        result.put("bookingRule",hosp.getBookingRule());
        //不需要重新返回
        hosp.setBookingRule(null);
        return result;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = getByHoscode(hoscode);
        if(hospital != null){
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    //根据医院编号获取医院预约挂号详情
    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    //获取查询list集合，遍历进行医院等级封装
    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        //查询省 市  地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hostypeString",hostypeString);
        return hospital;
    }

}
